package com.kkb.controller;

import com.kkb.common.utils.Constants;
import com.kkb.domain.AjaxResult;
import com.kkb.pojo.TbCategory;
import com.kkb.pojo.Ztree;
import com.kkb.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.kkb.domain.AjaxResult.error;

/**
 * 商品分类controller
 **/
@Api(value = "categoryController", tags = "商品分类")
@Controller
@RequestMapping("/categoryController")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/category")
    public String showCategory() {
        return "category/category";
    }


    //商品分类列表
    @ApiOperation("我的测试")
    @ResponseBody
    @RequestMapping("/getCategoryList")
    public List<TbCategory> getCategoryList(){
        List<TbCategory> category = categoryService.getCategory();
        return category;
    }

    /**
     * 新增商品分类
     */
@ApiOperation(value = "添加分类")
    @GetMapping("/add/{parentId}")
    public String  add(@PathVariable("parentId") Integer parentId, ModelMap mmap){
        mmap.put("category", categoryService.selectCategoryById(parentId));
        return "category/add";
    }
    /**
     * 新增商品分类
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TbCategory category) {
        return categoryService.insertCategory(category) > 0 ? AjaxResult.success() : error();
    }

    /**
     * 修改
     */
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Integer categoryId, ModelMap mmap)
    {
        TbCategory category = categoryService.selectCategoryById(categoryId);
        mmap.put("category", category);
        return "category/edit";
    }

    /**
     * 保存
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TbCategory category) {
        if (category.getCategoryId().equals(category.getParentId())) {
            return error("修改分类'" + category.getCategoryName() + "'失败，上级分类不能是自己");
        }
        return categoryService.updateCategory(category) > 0 ? AjaxResult.success() : error();
    }

    /**
     * 删除
     */
    @GetMapping("/remove/{categoryId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("categoryId") Integer categoryId) {
        if (categoryService.selectCount(categoryId) > 0) {
            return AjaxResult.warn("存在下级分类,不允许删除");
        }
        return categoryService.deleteCategoryById(categoryId) > 0 ? AjaxResult.success(): error();

    }

    /**
     * 校验部门名称
     */
    @PostMapping("/checkDeptNameUnique")
    @ResponseBody
    public String checkDeptNameUnique(TbCategory category) {
        return categoryService.checkDeptNameUnique(category.getCategoryName());
    }

    /**
     * 选择分类树
     *
     * @param categoryId 分类ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectDeptTree/{categoryId}", "/selectDeptTree/{categoryId}/{excludeId}" })
    public String selectDeptTree(@PathVariable("categoryId") Integer categoryId,
                                 @PathVariable(value = "excludeId", required = false) String excludeId, ModelMap mmap) {
        mmap.put("category", categoryService.selectCategoryById(categoryId));
        mmap.put("excludeId", excludeId);
        return "category/tree";
    }

    /**
     * 加载分类列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = categoryService.selectCategoryTree(new TbCategory());
        return ztrees;
    }
    /**
     * 根据级别获取分类
     */
    @GetMapping("/selectCategoryByLevel")
    @ResponseBody
    public AjaxResult selectCategoryByLevel(Integer level) {
        List<TbCategory> tbCategories = categoryService.selectCategoryByLevel(level);
        return  AjaxResult.success(tbCategories);
    }
}
